home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Super Collection / Windows 95 Super Collection.iso / win95 / utils / expreg4 / expreg4.cpp next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  3.8 KB  |  141 lines

  1. #include <afx.h>
  2. #include <stdio.h>
  3.  
  4. DWORD dwlName, dwlData, dwType;
  5. char szName[1024];
  6. char cbData[16384], *p;
  7. FILETIME fTime;
  8.  
  9. void TraverseKey (HKEY hKey, FILE *fo, CString keyName)
  10. {
  11.     HKEY hSubKey;
  12.     LONG lErr;
  13.     int nLine, ixVal;
  14.     DWORD dwX;
  15.     
  16.     fprintf (fo, "\n[%s]\n", (LPCSTR)keyName);
  17.  
  18.     // Enumerate values
  19.     for (ixVal=0;;ixVal++) {
  20.         dwlName = sizeof szName;
  21.         dwlData = sizeof cbData;
  22.         if ((lErr = RegEnumValue (hKey, ixVal, szName, &dwlName, NULL, &dwType, (LPBYTE)cbData, &dwlData)) != ERROR_SUCCESS)
  23.             break;
  24.         
  25.         fprintf (fo, "\"%s\"=", szName);
  26.         switch (dwType) {
  27.         case REG_DWORD:
  28.             fprintf (fo, "dword:%08X\n", *((DWORD*)cbData));
  29.             break;
  30.         case REG_SZ:
  31.             fprintf (fo, "\"%s\"\n", cbData);
  32.             break;
  33.         case REG_MULTI_SZ:
  34.         case REG_BINARY:
  35.         
  36.             fprintf (fo, "hex:");
  37.             nLine = 20;
  38.             for (dwX=0;dwX<dwlData;dwX++) {
  39.                 fprintf (fo, "%02x", (BYTE)cbData[dwX]);
  40.                 if (dwX<dwlData-1) fprintf (fo, ",");
  41.                 if (--nLine == 0) {
  42.                     nLine = 25;
  43.                     if (dwX<dwlData-1) fprintf (fo, "\\\n"); } }
  44.             fprintf (fo, "\n");
  45.             break;
  46.         default:
  47.             printf ("%s: vasue type %08X is unsupported\n", (LPCSTR)keyName, dwType); } }
  48.  
  49.     // Enumerate subkeys
  50.     for (ixVal=0;;ixVal++) {
  51.         dwlName = sizeof szName;
  52.         dwlData = sizeof cbData;
  53.         if ((lErr = RegEnumKeyEx (hKey, ixVal, szName, &dwlName, NULL, cbData, &dwlData, &fTime)) != ERROR_SUCCESS)
  54.             break;
  55.         
  56.         if (RegOpenKeyEx (hKey, szName, NULL, GENERIC_READ, &hSubKey) == ERROR_SUCCESS) {
  57.             TraverseKey (hSubKey, fo, keyName + '\\' + szName);
  58.             CloseHandle (hSubKey); }
  59.         else                                                                                     
  60.             printf ("Unable to open key %s for reading\n", keyName + '\\' + szName); }
  61. }    
  62.  
  63. HKEY OpenKey (LPCSTR szKey)
  64. {
  65.     CString strKey (szKey), strTopKey, strSubKey;
  66.     HKEY hKey, hSubKey;
  67.  
  68.     int ix = strKey.Find('\\');
  69.     if (ix > 0) {
  70.         strTopKey = strKey.Left (ix);
  71.         if (ix < strKey.GetLength()-1)
  72.             strSubKey = strKey.Mid (ix+1);
  73.         else
  74.             strSubKey.Empty (); }
  75.     else
  76.         return NULL;
  77.  
  78.     strTopKey.MakeUpper ();
  79.  
  80.     if (strTopKey == "HKEY_CLASSES_ROOT")
  81.         hKey = HKEY_CLASSES_ROOT;
  82.     else if (strTopKey == "HKEY_LOCAL_MACHINE")
  83.         hKey = HKEY_LOCAL_MACHINE;
  84.     else if (strTopKey == "HKEY_USERS")
  85.         hKey = HKEY_USERS;
  86.     else if (strTopKey == "HKEY_CURRENT_USER")
  87.         hKey = HKEY_CURRENT_USER;
  88.     else
  89.         return NULL;
  90.  
  91.     if (strSubKey.IsEmpty())
  92.         return hKey;
  93.     else if (RegOpenKeyEx (hKey, strSubKey, 0, GENERIC_READ, &hSubKey) == NO_ERROR)
  94.         return hSubKey;
  95.     else
  96.         return NULL;
  97. }
  98.  
  99. void usage (void)
  100. {
  101.     printf ("EXPREG4 utility exports given registry subtree on NT machine into an ASCII\n");
  102.     printf ("file suitable to export into Windows 95 registry by regedit utility\n\n");
  103.     printf ("Usage: expreg4 <key-name> [<output-file>]\n\n");
  104.     printf ("Give <key-name> in form <root-name>{/<subkey-name>}\n");
  105.     printf ("where <root-name> can be one of HKEY_LOCAL_MACHINE, HKEY_USERS,\n");
  106.     printf ("      HKEY_CURRENT_USER or HKEY_CLASSES_ROOT. Curly braces mean repetition\n");
  107.     printf ("      of enclosed expression 0 or more times. When <output-file> is not given,\n");
  108.     printf ("      then output goes to console.");
  109. }
  110.  
  111. void main (int argc, char **argv)
  112. {
  113.     HKEY hKey;
  114.     FILE *fo;
  115.     
  116.     printf ("EXPREG4: NT -> Windows 95 registry tree transfer utility\n");
  117.     printf ("Copyright (c) Elipromm Priv Co, 1995\n\n");
  118.  
  119.     if (argc < 2 || argc > 4) {
  120.         usage ();
  121.         printf ("usage: EXPREG4 <reg-key> [<output file>]\n");
  122.         return; }
  123.  
  124.     hKey = OpenKey (argv[1]);
  125.     if (hKey == NULL) {
  126.         printf ("Unable to open registry key %s\n", argv[1]);
  127.         return; }
  128.  
  129.     if (argc == 2)
  130.         fo = stdout;
  131.     else {
  132.         fo = fopen (argv[2], "w+");
  133.         if (fo == NULL) {
  134.             printf ("Unable to open outout file %s\n", argv[2]);
  135.             return; } }
  136.  
  137.     fprintf (fo, "REGEDIT4\n");
  138.  
  139.     TraverseKey (hKey, fo, CString(argv[1]));
  140. }
  141.